<?php
namespace Application\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Db\ResultSet\ResultSet;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Adapter\AdapterInterface;

class OrderTable
{

    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function getOrderbyId($id)
    {
        $id = (int) $id;
        $rowset = $this->tableGateway->select(array(
            'id' => $id
        ));
        $row = $rowset->current();
        if (! $row) {
            throw new \Exception("Could not find row option $id");
        }
        return $row;
    }
    public function getOrderbyTokenPayerid($token,$payerid)
    {
        $id = (int) $id;
        $rowset = $this->tableGateway->select(array(
            'token' => $token,
            'payerid'=>$payerid
        ));
        $row = $rowset->current();
        if (! $row) {
            return null;

        }
        return $row;
    }
    public function getOrderbySecurityCode($securityCode)
    {
        $id = (int) $id;
        $rowset = $this->tableGateway->select(array(
            'security_code'=>$securityCode
        ));
        $row = $rowset->current();
        if (! $row) {
            return null;

        }
        return $row;
    }



    public function fetchAll($paginated = false)
    {
        if ($paginated) {
            // create a new Select object for the table album
            $select = new Select('order');

            // create a new result set based on the Album entity
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new Order());
            // create a new pagination adapter object
            $paginatorAdapter = new DbSelect(
                // our configured select object
                $select,
                // the adapter to run it against
                $this->tableGateway->getAdapter(),
                // the result set to hydrate
                $resultSetPrototype);
            $paginator = new Paginator($paginatorAdapter);
            return $paginator;
        }
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }




    public function saveOrder(Order $order)
    {
        $data = array(
            'coupon_code' => $order->coupon_code,
            'first_name' => $order->first_name,
            'last_name' => $order->last_name,
            'email_address' => $order->email_address,
            'address' => $order->address,
            'city' => $order->city,
            'zip_code' => $order->zip_code,
            'country' => $order->country,
            'phone' => $order->phone,
            'paypal_code' => $order->paypal_code,
            'date_created' => $order->date_created,
            'date_updated' => $order->date_updated,
            'total_price' => $order->total_price,
            'token' => $order->token,
            'payer' => $order->payer,
            'payerid' => $order->payerid,
            'payerstatus' => $order->payerstatus,
            'payercountry' => $order->payercountry,
            'payerbusiness' => $order->payerbusiness,
            'ack' => $order->ack,
            'security_code' => $order->security_code,
            'status' => $order->status
        );

        $id = (int) $order->id;

        if ($id == 0) {
            $this->tableGateway->insert($data);
        } else {
            if ($this->getorderbyId($id)) {
                $this->tableGateway->update($data, array(
                    'id' => $id
                ));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
       return  $this->tableGateway->getLastInsertValue();
    }

    public function deleteOrder($id)
    {
        $this->tableGateway->delete(array(
            'id' => $id
        ));
    }
}